import { _decorator, Vec3 , Node, randomRangeInt} from 'cc';
import { SkillManager } from './SkillManager';
import { EGroup, EntityType, ESkill, ESkillType } from '../Config/Enum';
import ObjectPoolManager from '../Core/ObjectPoolManager';
import { Collider2D } from 'cc';
import { PlayerManager } from '../PlayerManager';
import { WudangBo } from './WudangBo';

const { ccclass } = _decorator;
    // rune1: "以静制动:第三击命中后，有10%几率立即重置调息", //阴阳流转
    // rune2: "以柔克刚:晕眩几率+10%", //以柔克刚
    // rune3: "剑气冲霄:第三击触发剑气，造成200%伤害",
@ccclass('WudangSkill1')
export class WudangSkill1 extends SkillManager {
    private _hasSplit = false;
    runeId = 0
    numOfBullets = 3; // 默认是3个
    level

    // 新增标志：是否是主子弹（由玩家直接发射的子弹）
    private isMasterBullet = true;


    init(type: ESkill, pm: PlayerManager) {
        // 如果是子子弹，就不要拿numOfBullets了,直接super.init(type, pm);直接释放
        if (!this.isMasterBullet) {
            super.init(type, pm);
            return;
        }

        // 如果是主子弹，就拿numOfBullets，并判断runeId，走逻辑，再释放
        if (this.isMasterBullet) {
            this._hasSplit = false;
        }

        //this.numOfBullets = pm.skills.get(type).numOfBullets +  pm.skills.get(type).level; // 获取当前武学的符文ID

        //this.reset();
        super.init(type, pm);

        if (!this._hasSplit) {

                    this.scheduleOnce(() => {
                        this.castMoretimes(1); 
                    }, 0.2); // 延迟0.3秒释放
            

        }




        
        // 其他符文效果
        switch (this.runeId) {
            case 1:
           

                break;
            case 2:
                // 穿透效果在碰撞处理中实现
                this.effectChance = 0.2
                break;  
            case 3:
                break;
            default:
                //console.log("WudangSkill1 no rune", this.runeId);
                break;
        }

    }

    protected onHitEnemy(self, other: Collider2D): void {
        // 先调用父类方法处理伤害
        super.onHitEnemy(self, other);
        
        switch (this.runeId) {
            case 1:

                if(Math.random()<0.5 && !this._hasSplit){
                    this.scheduleOnce(() => {
                        this.castMoretimes(1);
                    }, 0.2); // 延迟0.3秒释放
                    console.log("WudangSkill1 rune1 castMoretimes");
                }
                break;
            case 2:
                break;  
            case 3:
                if(Math.random()<0.5){
                    this.createBo()
                }
                break;
            default:
                //console.log("WudangSkill1 no rune", this.runeId);
                break;
        }

    }

    /**
     * 连续释放多次子弹，同一角度
     * @param count 子弹数量
     */
    castMoretimes(numOfBullets: number) {
        // 往同一个direction方向发射numOfBullets个子弹，间隔0.1秒
        const interval = 0.1; // 间隔时间
        for (let i = 0; i < numOfBullets; i++) {
            setTimeout(() => {
                this.createBullet(this.direction);
            }, i * interval * 1000); // 转换为毫秒
        }


        
    }

    /**
     * 创建新子弹
     * @param direction 子弹方向
     */
    private createBullet(direction: Vec3) {
        const bulletNode = ObjectPoolManager.Instance.get(this.type as EntityType);
        bulletNode.setWorldPosition(this.pm.node.worldPosition);
        //bulletNode.setWorldPosition(200,399,0);
        const bullet = bulletNode.getComponent(WudangSkill1);
        if (!bullet) {
            console.error(`无法获取TangmenSkill1组件，类型: ${this.type}`);
            return;
        }

        // 标记为子子弹（非主子弹）
        bullet.isMasterBullet = false;
        // 标记为已分裂（防止子子弹再次分裂）
        bullet._hasSplit = true;


        // 继承原始属性
        bullet.init(this.type, this.pm);
        bullet.setDirection(direction);
        
        // 应用符文效果到子子弹
        if (this.runeId === 3) {
            //bullet.damage = this.damage; // 继承主子弹的伤害值
        }
    }



createBo(){
    
    let explosionNode = ObjectPoolManager.Instance.get(ESkill.WudangBo) as Node;
    explosionNode.setWorldPosition(this.pm.node.worldPosition);

    // 传递必要参数给爆炸实体
    const explosionSkill = explosionNode.getComponent(WudangBo);
    if (explosionSkill) {
        explosionSkill.level = this.level;
        explosionSkill.runeId = this.runeId;
        explosionSkill.damage = this.damage*2; // 传递基础伤害
        explosionSkill.playSpeed = 0.1; // 
        explosionSkill.criticalChance = 0; // 
        explosionSkill.scale = 3; // 爆炸大小
        //explosionSkill.direction = direction
        explosionSkill.init(ESkill.WudangBo, this.pm);
        explosionSkill.setDirection(this.direction)
        //explosionSkill.pm = this.pm; // 标记为子子弹
        //explosionSkill.duration = false; // 标记为子子弹

    }


    ObjectPoolManager.Instance.ret(this.node);

}




}